Velocity is defined as
$$\vec{v}=\frac{d\vec{r}}{dt}.$$In Cartesian coordinates,
$$\vec{r}=x\hat{x}+y\hat{r}+z\hat{z}\, .$$Since $\hat{x}$, $\hat{y}$, and $\hat{z}$ are constant in time, then their time-derivatives are zero and we can write the velocity as
$$\vec{v}=\frac{dx}{dt}\hat{x} + \frac{dy}{dt}\hat{y} + \frac{dz}{dt}\hat{z}\, .$$If you know $\vec{r}$ as a function of time, then you can take the derivative to get $\vec{v}$ as a function of time.
Suppose that the position of a particle is:
$$\vec{r}(t)=R\cos(\omega t)\hat{x} + R\sin(\omega t)\hat{y}$$where $R=1$ m and $\omega=\pi$ rad/s. What is $\vec{v}(t)$? Write an iVisual program to animate the motion of the particle. Draw an arrow for the position of the particle. Draw an arrow for the velocity of the particle. You may have to scale the velocity arrow to make its size appropriate for the screen.
The derivative is
$$\vec{v}=\frac{d\vec{r}}{dt}=-R\omega\sin(\omega t)\hat{x} + R\omega\cos(\omega t)\hat{y}$$Now let's write a program to simulate the motion of the particle.
In [12]:
from __future__ import division, print_function
from ivisual import *
from math import *
In [19]:
scene = canvas(title="Animation of Particle's Motion")
t=0
dt=0.01
R=1
omega=pi
r=R*vector(cos(omega*t), sin(omega*t),0)
v=R*omega*vector(-sin(omega*t), cos(omega*t), 0)
particle=sphere(pos=r, radius=R/10, color=color.red, make_trail=True)
sw=R/20
rarrow=arrow(pos=(0,0,0), axis=r, shaftwidth=sw, color=color.yellow)
vscale=0.2
varrow=arrow(pos=r, axis=vscale*v, shaftwidth=sw, color=color.magenta)
while t<4:
rate(100)
r=R*vector(cos(omega*t), sin(omega*t),0)
v=R*omega*vector(-sin(omega*t), cos(omega*t), 0)
particle.pos=r
rarrow.axis=r
varrow.pos=r
varrow.axis=vscale*v
t=t+dt
Starting with the definition of velocity, then
$$d\vec{r}=\vec{v}dt$$and
$$\vec{r}= \vec{r}_0 + \int \vec{v}dt\, .$$We can solve this analytically if we know $\vec{v}(t)$ or we can solve it numerically by summing $\vec{v}\Delta t$ for many small time steps, $\Delta t$. The position at any time $t+\Delta t$ is
$$\vec{r}_{(t+\Delta t)} = \vec{r}_{(t)} + \vec{v}\Delta t\, .$$Note that $\vec{v}$ is not necessarily constant, so should you use $\vec{v}_t$ (the velocitiy at the beginning of the time interval) or $\vec{v}_{(t+\Delta t)}$ (the velocity at the end of the time interval) or perhaps an average of the two. A fairly accurate and simple method is called the Euler-Cromer method. In this case, you evaluate the velocity at the end of the time interval first and then use this velocity to find the position. Thus,
$$\vec{r}_{(t+\Delta t)} = \vec{r}_{(t)} + \vec{v}_{(t+\Delta t)}\Delta t\, .$$By repeating this calculation in a loop from $t=0$ to $t=t_f$, you are effectively integrating numerically.
Suppose that a particle has a velocity
$$\vec{v}=A\hat{x} + B\cos(\omega t)\hat{y}$$where $A=0.5$ m/s, $B=1$ m/s, and $\omega=2\pi$ rad/s. Assume that at $t=0$, $\vec{r}=<0,0,0>$.
Use numerical integration to find its position $\vec{r}$ as a function of time. Graph $x(t)$ and $y(t)$ using matplotlib. Analytically find the function $\vec{r}(t)$ and compare the result to the graphs.
In [31]:
from __future__ import division, print_function
from ivisual import *
from math import *
In [36]:
scene2=canvas(title="Integrating velocity to calculation position")
t=0
dt=0.01
A=0.5
B=1
omega=2*pi
r=vector(0,0,0)
v=vector(A,B*cos(omega*t),0)
particle=sphere(pos=r, radius=1/20, color=color.red, make_trail=True)
scene2.range=1
#store data in lists for graphing
tlist=[]
xlist=[]
ylist=[]
while t<4:
rate(100)
#calculate v(t)
v=vector(A,B*cos(omega*t),0)
#update position
r=r+v*dt
particle.pos=r
#update clock
t=t+dt
#append data to lists
tlist.append(t)
xlist.append(r.x)
ylist.append(r.y)
#print(tlist)
#print(xlist)
#print(ylist)
In [37]:
%matplotlib inline
In [38]:
import matplotlib.pyplot as plt
In [39]:
plt.title('x vs t')
plt.xlabel('time (s)')
plt.ylabel('x (m)')
plt.plot(tlist, xlist, 'b.')
plt.show()
In [40]:
plt.title('y vs t')
plt.xlabel('time (s)')
plt.ylabel('y (m)')
plt.plot(tlist, ylist, 'r.')
plt.show()
Integrating analytically gives:
$$\vec{r}= \vec{r}_0 + \int \vec{v}dt$$$$\vec{r}= \int \vec{v}dt$$$$\vec{r}= \int_0^t (A\hat{x} + B\cos(\omega t)\hat{y})dt$$$$\vec{r}= At\hat{x} + \frac{B}{\omega}\sin(\omega t)\hat{y} $$If we substitute the constants $A=0.5$ m/s, $B=1$ m/s, and $\omega=2\pi$ rad/s, then the analytic function shows that a graph of $x(t)$ should be linear with a slope of 0.5 m/s. This agrees with the graph of x(t) data shown above. The analytic function shows that $y(t)$ should be a sinusoidal graph (so $y=0$ at $t=0$) with a maximum value of $y=\frac{1}{2\pi}=0.16$ m. This agrees with the graph of $y(t)$ data shown above.
In [ ]: